package com.jt.service;

import com.jt.mapper.ItemDescMapper;
import com.jt.pojo.Item;
import com.jt.pojo.ItemDesc;
import com.jt.vo.EasyUITable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jt.mapper.ItemMapper;

import java.util.Arrays;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService {
	
	@Autowired
	private ItemMapper itemMapper;
	@Autowired
	private ItemDescMapper itemDescMapper;


	@Override
	public EasyUITable findItemByOage(Integer page, Integer rows) {
		int startIndex = (page - 1) * rows;
		List<Item> itemList = itemMapper.findItemByPage(startIndex, rows);
		int count = itemMapper.selectCount(null);
		return new EasyUITable(count, itemList);
	}

	@Override
	public void saveItem(Item item, ItemDesc itemDesc) {
		item.setStatus(1);
		itemMapper.insert(item);
		itemDesc.setItemId(item.getId());
		itemDescMapper.insert(itemDesc);
	}

	@Override
	public void updateItem(Item item, ItemDesc itemDesc) {
		itemMapper.updateById(item);
		itemDesc.setItemId(item.getId());
		itemDescMapper.updateById(itemDesc);
	}

	@Override
	public void deleteItemById(Long[] ids) {
		List<Long> longList = Arrays.asList(ids);
		itemMapper.deleteItemById(ids);
		itemDescMapper.deleteBatchIds(longList);
	}

	@Override
	public void updateStatus(Integer status, Long[] ids) {
		// mp
//		Item item = new Item();
//		item.setStatus(status);
//		UpdateWrapper<Item> updateWrapper = new UpdateWrapper<>();
//		updateWrapper.in("id", ids);
//		itemMapper.update(item, updateWrapper);
		// 手写sql
		itemMapper.updateStatus(status, ids);
	}

	@Override
	public ItemDesc findItemDescById(Long itemId) {
		// 为什么要强转
		return itemDescMapper.selectById(itemId);

	}

}
